import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;

import cn.seaboot.commons.file.MediaType;
import cn.seaboot.commons.ftp.Ftp;
import cn.seaboot.commons.ftp.FtpSource;
import cn.seaboot.commons.ftp.FtpProperties;
import cn.seaboot.commons.ftp.pool.FtpPooledSource;

/**
 * @author Mr.css
 * @version 2022-11-14 10:23
 */
public class FtpExample {

    public static void main1(String[] args) throws IOException {
        FtpProperties properties = new FtpProperties();
        properties.setHost("127.0.0.1");
        properties.setUsername("root");
        properties.setPassword("root");
        FtpPooledSource ftpClient = new FtpPooledSource(properties);

        String[] names;
        try (Ftp ftp = ftpClient.build()) {
            System.out.println(ftp.pwd());
            names = ftp.cd("ftp/hes").listNames();
            System.out.println(Arrays.toString(names));
        }

        File root = new File("C:\\Users\\ASUS\\Desktop\\test");
        int cnt = 1;
        while (cnt-- > 0) {
            int finalCnt = cnt;
            new Thread(() -> {
                for (String name : names) {
                    File out = new File(root, finalCnt + "_" + name);
                    try (Ftp ftp = ftpClient.build();
                         FileOutputStream os = new FileOutputStream(out)) {
                        ftp.cd("ftp/hes").download(name, os);
                    } catch (IOException e) {
                        e.printStackTrace();
                        System.out.println(finalCnt);
                    }
                }
            }).start();
        }
        ftpClient.close();
    }

    public static void main2(String[] args) throws IOException {
        FtpProperties properties = new FtpProperties();
        properties.setHost("127.0.0.1");
        properties.setPort(21);
        properties.setUsername("root");
        properties.setPassword("root");
        properties.setPassiveMode(true);
        properties.setEncoding(Charset.defaultCharset().name());

        FtpSource ftpSource = new FtpSource(properties);

        try(Ftp ftp = ftpSource.build()){
            System.out.println(ftp.pwd());
            String[] names = ftp.listNames();
            System.out.println(Arrays.toString(names));

            ftp.cd("ftp");

            try (FileOutputStream os = new FileOutputStream(new File("C:\\Users\\ASUS\\Desktop\\testtest.xls"))) {
                ftp.download("abc20230706094050.xlsx", os);
            }
        }
    }
}
